package cn.xscrum.vtsp.controller;

import cn.xscrum.common.annotation.Log;
import cn.xscrum.common.core.controller.BaseController;
import cn.xscrum.common.core.domain.AjaxResult;
import cn.xscrum.common.core.page.TableDataInfo;
import cn.xscrum.common.core.text.Convert;
import cn.xscrum.common.enums.BusinessType;
import cn.xscrum.common.utils.poi.ExcelUtil;
import cn.xscrum.vtsp.domain.*;
import cn.xscrum.vtsp.service.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 活动管理Controller
 *
 * @author zfc
 * @date 2021-10-06
 */
@Controller
@RequestMapping("/vtsp/publish")
public class VtspPublishActivity extends BaseController {

    private String prefix = "vtsp/publish";
    @Autowired
    private IVtspActivityService vtspActivityService;
    @Autowired
    private IVtspTypeService typeService;
    @Autowired
    private IVtspPostService postService;
    @Autowired
    private IVtspAdviserService vtspAdviserService;
    @Autowired
    private IVtspEnrollService enrollService;
    @Autowired
    private IVtspVolunteerService volunteerService;
    @Autowired
    private IVtspAppraiseService appraiseService;

    /**
     * 初始加载的志愿者信息列表
     */
    @RequiresPermissions("vtsp:publish:view")
    @GetMapping()
    public String volunteer() {
        return prefix + "/publish";
    }
    /**
     * 活动记录
     */
    @GetMapping("/record")
    public String record() {
        return prefix + "/record";
    }
    /**
     * 活动顾问
     */
    @GetMapping("/add")
    public String add(ModelMap mmap) {
        List<VtspType> typeList = typeService.selectVtspTypeList(null);
        List<VtspAdviser> advisers = vtspAdviserService.selectVtspAdviserList(null);
        mmap.put("typeList", typeList);
        mmap.put("advisers", advisers);
        return prefix + "/add";
    }
    /**
     * 增加活动岗位
     */
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@RequestBody ActivityPost activityPost) {
        VtspActivity activity = new VtspActivity();
        BeanUtils.copyProperties(activityPost,activity);
        VtspType type = typeService.selectVtspTypeById(activity.getTypeId());
        activity.setTypeName(type.getTypeName());
        int i = vtspActivityService.insertVtspActivity(activity);

        List<VtspPost> posts = activityPost.getPosts();
        System.err.println(posts);
        posts.forEach(post -> {
            post.setActivityId(activity.getActivityId());
            postService.insertVtspPost(post);
        });
        return toAjax(i);
    }
    /**
     * 结项
     */
    @GetMapping("/end/{id}")
    public String end(@PathVariable Long id, Model model) {
        VtspActivity vtspActivity = vtspActivityService.selectVtspActivityById(id);
        model.addAttribute("vtspActivity", vtspActivity);
        return prefix + "/end";
    }
    /**
     * 发布
     */
    @PostMapping("/publish")
    @ResponseBody
    public AjaxResult publish(String ids) {
        Long[] idArr = Convert.toLongArray(ids);
        VtspActivity activity = new VtspActivity();
        int i=0;
        for (Long id : idArr) {
            activity.setActivityId(id);
            activity.setActivityState(2);
            i+=vtspActivityService.updateVtspActivity(activity);
        }
        return toAjax(i);
    }
    /**
     * 结项时，把已录用志愿者插入appraise表中，用于评价
     */
    @PostMapping("/end")
    @ResponseBody
    public AjaxResult endSave(VtspActivity activity) {
        if (activity.getActivityState() == 6) {
            VtspEnroll enroll = new VtspEnroll();
            enroll.setActivityId(activity.getActivityId());
            List<VtspEnroll> enrollList = enrollService.selectVtspEnrollList(enroll);
            VtspAppraise appraise = new VtspAppraise();
            for (VtspEnroll vtspEnroll : enrollList) {
                appraise.setActivityId(activity.getActivityId().toString());
                appraise.setActivityName(activity.getActivityName());
                appraise.setEnrollId(vtspEnroll.getEnrollId());
                appraise.setAppraiseState(1);
                Long volunteerId = vtspEnroll.getVolunteerId();
                appraise.setVolunteerId(volunteerId.toString());
                VtspVolunteer volunteer = volunteerService.selectVtspVolunteerById(volunteerId);
                appraise.setVolunteerName(volunteer.getVolunteerName());
                int i = appraiseService.insertVtspAppraise(appraise);
            }
        }
        return toAjax(vtspActivityService.updateVtspActivity(activity));
    }
    /**
     * 删除
     */
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(vtspActivityService.deleteVtspActivityByIds(ids));
    }
    /**
     * 编辑
     */
    @GetMapping("/edit/{activityId}")
    public String edit(@PathVariable("activityId") Long activityId, ModelMap mmap) {
        VtspActivity activity = vtspActivityService.selectVtspActivityById(activityId);
        VtspPost post = new VtspPost();
        post.setActivityId(activityId);
        List<VtspPost> postList = postService.selectVtspPostList(post);
        List<VtspType> typeList = typeService.selectVtspTypeList(null);
        ActivityPost activityPost = new ActivityPost();
        BeanUtils.copyProperties(activity,activityPost);
        activityPost.setPosts(postList);
        List<VtspAdviser> advisers = vtspAdviserService.selectVtspAdviserList(null);
        mmap.put("advisers", advisers);
        mmap.put("typeList", typeList);
        mmap.put("vtspActivity", activityPost);
        return prefix + "/edit";
    }
    /**
     * 编辑保存
     */
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@RequestBody ActivityPost activityPost) {
        VtspActivity activity = new VtspActivity();
        BeanUtils.copyProperties(activityPost,activity);
        VtspType type = typeService.selectVtspTypeById(activity.getTypeId());
        activity.setTypeName(type.getTypeName());
        int i = vtspActivityService.updateVtspActivity(activity);

        List<VtspPost> posts = activityPost.getPosts();
        posts.forEach(post -> {
            post.setActivityId(activity.getActivityId());
            postService.updateVtspPost(post);
        });
        return toAjax(i);
    }
    /**
     * 评价
     */
    @GetMapping("/appraise/{activityId}")
    public String appraise(@PathVariable Long activityId, ModelMap mmap) {
        mmap.addAttribute("activityId", activityId);
        return prefix + "/appraise";
    }
    /**
     * 报名
     */
    @RequestMapping("/toEnroll/{activityId}")
    public String toEnroll(@PathVariable Long activityId, ModelMap mmap) {
        mmap.addAttribute("activityId", activityId);
        return prefix + "/enroll";
    }
    /**
     * 服务对象
     */
    @GetMapping("/recipient/{activityId}")
    public String recipient(@PathVariable Long activityId, ModelMap mmap) {
        mmap.addAttribute("activityId", activityId);
        return prefix + "/recipient";
    }

    /**
     * 活动导入
     */
    @RequiresPermissions("vtsp:activity:import")
    @Log(title = "活动导入", businessType = BusinessType.IMPORT)
    @PostMapping("/importList")
    @ResponseBody
    public AjaxResult importList(MultipartFile file) throws Exception
    {
        //创建Excel工具类
        ExcelUtil<VtspActivity> util = new ExcelUtil<VtspActivity>(VtspActivity.class);
        //选择导入文件
        List<VtspActivity> list = util.importExcel(file.getInputStream());
        //导入文件
        String message = vtspActivityService.importList(list);
        return AjaxResult.success(message);
    }

    @GetMapping("/importTemplate")
    @ResponseBody
    public AjaxResult importTemplate() {
        ExcelUtil<VtspActivity> util = new ExcelUtil<>(VtspActivity.class);
        return util.importTemplateExcel("活动数据");
    }

    /**
     *列表展示
     */
    @RequiresPermissions("vtsp:publish:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(VtspActivity vtspActivity) {
        startPage();
        List<VtspActivity> list = vtspActivityService.selectVtspActivityList(vtspActivity);
        System.out.println(list);
        return getDataTable(list);
    }

}
